<?php


namespace api\modules\v1\controllers\buss;

use Yii;
use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use services\buss\PlatformService;

class PlatformController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/platform/index 平台列表
     * @apiGroup Platform
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {String} keywords               搜索词
     * @apiParam {int} platform_content_type     内容类型
     * @apiParam {int} status                    状态
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               列表ID
     * @apiSuccess {String}  data.list.name             名称
     * @apiSuccess {Number}  data.list.platform_content_type             平台内容类型ID
     * @apiSuccess {Number}  data.list.platform_content_type_str         平台内容类型
     * @apiSuccess {Number}  data.list.authenticator_rules             规则
     * @apiSuccess {String}  data.list.operation_name   创建人
     * @apiSuccess {Number}  data.list.operation_id     创建人ID
     * @apiSuccess {Number}  data.list.sort             排序
     * @apiSuccess {String}  data.list.created_at_str   创建日期
     * @apiSuccess {String}  data.list.updated_at_str   更新日期
     * @apiSuccess {String}  data.condition
     * @apiSuccess {String}  data.condition.type_arr             类型筛选
     * @apiSuccess {String}  data.condition.status_arr           状态筛选
     *
     */
    public function actionIndex()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $platformService = new PlatformService();
        $list = $platformService->platformList($params, $page, $pageSize);
        $list['condition'] = [
            'status_arr' => PlatformService::getStatusCondition(),
            'type_arr' => PlatformService::getTypeCondition(),
        ];
        return $list;
    }

    /**
     * @api {post} /v1/buss/platform/update-platform 更新平台
     * @apiGroup Platform
     * @apiName update-platform
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                                      平台ID
     * @apiBody {String}   name                                    平台名称
     * @apiBody {Number}   platform_content_type                   平台内容类型【多个逗号隔开】
     * @apiBody {String}   authenticator_rules                     认证规则
     * @apiBody {Number}   status                                  状态【-1：删除、0：禁用、1：启用】
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdatePlatform()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['name']) || !$params['name']) {
            return CommonHelper::api(422, '名称必填');
        }
        $params['id'] = $params['id'] ?? 0;
        $platformService = new PlatformService();
        return $platformService->updatePlatform($params);
    }

    /**
     * @api {post} /v1/buss/platform/delete-platform 删除平台
     * @apiGroup Platform
     * @apiName delete-platform
     * @apiUse CommonParam
     *
     * @apiBody {String} id     ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDeletePlatform()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $platformService = new PlatformService();
        return $platformService->deletePlatform($params);
    }
}
